﻿using Nova_BLL;
using Nova_Common.Extend;
using Nova_MOD;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WMS_Api.App_Start;
using WMS_Api.Areas.RFSystem.Models;
using WMS_Api.Controllers;
using WMS_Api.Models;

namespace WMS_Api.Areas.RFSystem.Controllers
{
    /// <summary>
    /// 手持特殊物料组盘功能
    /// </summary>
    [RoutePrefix("api/RFSystem/SpecialPackage")]
    [ControllerGroup("PDA手持", "特殊物料组盘功能接口")]
    public class SpecialPackageController : BaseApiController
    {
        RFBll RfBll = new RFBll();

        /// <summary>
        /// 获取特殊物料列表
        /// </summary>
        /// <returns></returns>
        [HttpGet, Route("GetMaterialList")]
        public ApiResult GetMaterialList(int groupType)
        {
            TBase_MaterialBll bll = new TBase_MaterialBll();
            var list = bll.GetListByGroupType(groupType);
            return Success("获取特殊物料列表!", list);
        }

        /// <summary>
        ///  获取托盘组盘的特殊物料信息
        /// </summary>
        /// <param name="trayId">托盘编码</param>
        /// <returns></returns>
        [HttpGet, Route("GetTraySpecialMaterialList")]
        public ApiResult GetTraySpecialMaterialList(string trayId)
        {
            if (trayId.IsEmpty())
                return Error(ResultCode.ParaError, "托盘编号不能为空!");
            DataTable dt = RfBll.GetTraySpecialMaterialInfor(trayId);
            List<Tray_SpecialMaterialInfoModel> list = Common.DataTableToList<Tray_SpecialMaterialInfoModel>(dt);
            return Success("获取托盘组盘的特殊物料信息!", list);
        }

        /// <summary>
        /// 判断托盘是否可以组盘
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpPost, Route("IsSetPackage")]
        public ApiResult IsSetPackage(SetPackageModel param)
        {
            if (param.StrTrayId.IsEmpty())
                return Error(ResultCode.ParaError, "StrTrayId不能为空!");
            if (param.MaterialCode.IsEmpty())
                return Error(ResultCode.ParaError, "MaterialCode不能为空!");
            if (param.Size <= 0)
                return Error(ResultCode.ParaError, "Size不能为空!");
            //DataTable dt = RfBll.TrayCanPackage(param.StrTrayId);
            //if (dt.Rows.Count > 0)
            //{
            //    if (dt.Rows[0][0].ToString() != "0")
            //    {
            //        return Error(ResultCode.Error, "该托盘已经在立体库，不允许再次组盘！");
            //    }
            //}
            DataTable dt = RfBll.GetTraySpecialMaterialInfor(param.StrTrayId);
            TBase_MaterialBll materialBll = new TBase_MaterialBll();
            var model = materialBll.GetByMatCode(param.MaterialCode);
            if (dt.Rows.Count > 0)
            {
                if (dt.Rows[0]["MaterialGroupType"].ToString() != model.GroupType.ToString())
                {
                    return Error(ResultCode.Error, "托盘只能绑定一种特殊物品类型！");
                }
            }
            return Success();
        }

        /// <summary>
        /// 根据入库单据组盘-绑定托盘和物料
        /// </summary>
        /// <param name="param">组盘参数</param>
        /// <returns></returns>
        [HttpPost, Route("SetPackageSpecial")]
        public ApiResult SetPackageSpecial(SetPackageModel param)
        {
            StoreProceResult res = RfBll.SetPackageSpecial(param.StrTrayId, param.MaterialCode, param.MatNum, param.Size);
            if (res.ResNo == 1)
                return Success("组盘成功!");
            else
                return Error(ResultCode.Error, res.ResMsg);
        }

        /// <summary>
        /// 解盘-解绑托盘和物料的关系
        /// </summary>
        /// <param name="param">解盘参数</param>
        /// <returns></returns>
        [HttpPost, Route("PackageDelSpecial")]
        public ApiResult PackageDelSpecial(SetPackageModel param)
        {
            string message = string.Empty;
            StoreProceResult res = RfBll.PackageDelSpecial(param.StrTrayId, param.MaterialCode, param.MatNum);
            if (res.ResNo == 1)
                return Success("解绑成功!");
            else
                return Error(ResultCode.OtherError, res.ResMsg);
        }
    }
}
